﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using ServiceConsumer;

/// <summary>
/// Summary description for CarrierServices
/// </summary>
public class CarrierServices
{
    private string sUrlWS;
    private string sUserName;
    private string sPassWord;

    private string sFromCity;

    private int iCarrierID;

    public CarrierServices()
    {
        NhaKinhDoanhDataContext db = new NhaKinhDoanhDataContext();

        var query = (from nvc in db.NhaVanChuyens
                     where nvc.DefaultCarrier == 1
                     select nvc).Single();

        sUrlWS = query.LinkWS;
        sUserName = query.UserName;
        sPassWord = query.PassWord;
        sFromCity = query.CiTyID;

        iCarrierID = query.CarrierID;
    }

    public CarrierServices(int CarrierID)
    {
        iCarrierID = CarrierID;

        NhaKinhDoanhDataContext db = new NhaKinhDoanhDataContext();

        var query = (from nvc in db.NhaVanChuyens
                     where nvc.CarrierID == iCarrierID
                     select nvc).Single();

        sUrlWS = query.LinkWS;
        sUserName = query.UserName;
        sPassWord = query.PassWord;
        sFromCity = query.CiTyID;
    }

    public int GetCarrierID()
    {
        return iCarrierID;
    }

    public DataTable CitiesList()
    {
        try
        {
            object oCities = WSProxy.CallWebService(sUrlWS, "CitiesList", null);
            DataTable dtCities = WSProxy.ConvertType<DataTable>(oCities);

            return dtCities;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public string WSLogin()
    {
        try
        {
            List<object> lstParas = new List<object>();
            lstParas.Add(sUserName);
            lstParas.Add(sPassWord);

            object oSID = WSProxy.CallWebService(sUrlWS, "WSLogin", lstParas.ToArray());
            string sSID = oSID.ToString();

            return sSID;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public DataTable Route(string sToCityID)
    {
        try
        {
            List<object> lstParas = new List<object>();
            lstParas.Add(sFromCity);
            lstParas.Add(sToCityID);

            object oRoute = WSProxy.CallWebService(sUrlWS, "Route", lstParas.ToArray());
            DataTable dtRoute = WSProxy.ConvertType<DataTable>(oRoute);

            return dtRoute;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public float RoutePrice(string sToCityID, string sDemension, int iTransportType)
    {
        try
        {
            List<object> lstParas = new List<object>();
            lstParas.Add(sFromCity);
            lstParas.Add(sToCityID);
            lstParas.Add(sDemension);
            lstParas.Add(iTransportType);

            object oSID = WSProxy.CallWebService(sUrlWS, "RoutePrice", lstParas.ToArray());

            float fPrice = float.Parse(oSID.ToString());

            return fPrice;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public string RoutePrice2(string sToCityID, string sDemension, int iTransportType, DateTime dDateStartTransport)
    {
        try
        {
            List<object> lstParas = new List<object>();

            string sSID = WSLogin();
            lstParas.Add(sSID);

            lstParas.Add(sFromCity);
            lstParas.Add(sToCityID);
            lstParas.Add(sDemension);
            lstParas.Add(iTransportType);
            lstParas.Add(dDateStartTransport);

            object oResult = WSProxy.CallWebService(sUrlWS, "RoutePrice2", lstParas.ToArray());

            string sResult = oResult.ToString();

            return sResult;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}